package com.stackexchange.webapis
{
	import com.adobe.serialization.json.JSON;
	import com.stackexchange.webapis.enums.PostType;
	import com.stackexchange.webapis.enums.RankType;
	import com.stackexchange.webapis.enums.UserType;
	import com.stackexchange.webapis.methodgroups.ResponseParser;
	
	import flexunit.framework.Assert;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNull;
	import org.hamcrest.date.dateEqual;
	
	public class ResponseParserTest
	{		
		private var _parser:ResponseParser;
		
		[Before]
		public function setUp():void
		{
			_parser = new ResponseParser();
		}
		
		[After]
		public function tearDown():void
		{
			_parser = null;
		}
		
		[Ignore]
		[Test]
		public function testParseAnswers():void
		{
			var rawAnswers:String = '{   "total": 2,   "page": 1,   "pagesize": 30,   "answers": [     {       "answer_id": 4535096,       "accepted": true,       "answer_comments_url": "/answers/4535096/comments",       "question_id": 4535053,       "owner": {         "user_id": 220819,         "user_type": "registered",         "display_name": "Jacob Relkin",         "reputation": 30002,         "email_hash": "0135bb75b1f08484d26239e8117b111c"       },       "creation_date": 1293391412,       "last_edit_date": 1293392295,       "last_activity_date": 1293392295,       "up_vote_count": 4,       "down_vote_count": 0,       "view_count": 33,       "score": 4,       "community_owned": false,       "title": "ActionScript 3: Unexpected behaviour of array, when pushing an element"     },     {       "answer_id": 4535105,       "accepted": false,       "answer_comments_url": "/answers/4535105/comments",       "question_id": 4535053,       "owner": {         "user_id": 541804,         "user_type": "registered",         "display_name": "FlexJack",         "reputation": 57,         "email_hash": "33f442b4b3645fc949e9895a74a3d2b9"       },       "creation_date": 1293391568,       "last_activity_date": 1293391568,       "up_vote_count": 1,       "down_vote_count": 0,       "view_count": 33,       "score": 1,       "community_owned": false,       "title": "ActionScript 3: Unexpected behaviour of array, when pushing an element"     }   ] }';
			var answerList:AnswerList = _parser.parseAnswers(JSON.decode(rawAnswers));
			assertEquals(answerList.total, 2);
			assertEquals(answerList.page, 1);
			assertEquals(answerList.pageSize, 30);
			assertEquals(answerList.answers, 2);
			
		}
		
		[Ignore]
		[Test]
		public function testParseComment():void
		{
			Assert.fail("Test method Not yet implemented");
		}
		
		[Test]
		public function testParseComments():void
		{
			var rawComments:String = '{   "total": 1,   "page": 1,   "pagesize": 30,   "comments": [     {       "comment_id": 4969417,       "creation_date": 1293393856,       "owner": {         "user_id": 68909,         "user_type": "registered",         "display_name": "macke",         "reputation": 1297,         "email_hash": "7678b64c87d74610f53e6106c8ffc1e1"       },       "post_id": 4535053,       "post_type": "question",       "score": 0,       "body": "All answers given are absolutely correct, it&#39;s a very common error. I do it all the time, there really should be a compiler warning for it."     }   ] }';
			
			var commentList:CommentList = _parser.parseComments(JSON.decode(rawComments));
			assertEquals(commentList.total, 1);
			assertEquals(commentList.page, 1);
			assertEquals(commentList.pageSize, 30);
			var comment:Comment = commentList.comments[0] as Comment
			assertEquals(comment.commentId, 4969417);
			assertEquals(comment.creationDate.time, _parser.parseDate(1293393856).time);
			assertEquals(comment.postId, 4535053);
			assertEquals(comment.postType, PostType.QUESTION);
			assertEquals(comment.score, 0);
			assertEquals(comment.body, "All answers given are absolutely correct, it&#39;s a very common error. I do it all the time, there really should be a compiler warning for it.");
			assertEquals(comment.owner.displayName, "macke");
			assertEquals(comment.owner.emailHash, "7678b64c87d74610f53e6106c8ffc1e1");
			assertEquals(comment.owner.reputation, 1297);
			assertEquals(comment.owner.userId, 68909);
			assertEquals(comment.owner.userType, UserType.REGISTERED);
		}
		
		[Test]
		public function testParseBadges():void
		{
			var rawComments:String = '{   "badges": [     {       "badge_id": 204,       "rank": "silver",       "name": ".htaccess",       "description": "Earned 400 upvotes for answers in the .htaccess tag",       "award_count": 1,       "tag_based": true,       "badges_recipients_url": "/badges/204"     }   ] }';
			
			var badgeList:BadgeList = _parser.parseBadges(JSON.decode(rawComments));
			assertEquals(badgeList.total, 0);
			assertEquals(badgeList.page, 0);
			assertEquals(badgeList.pageSize, 0);
			var badge:Badge = badgeList.badges[0] as Badge;
			assertEquals(badge.badgeId, 204);
			assertEquals(badge.rank, RankType.SILVER);
			assertEquals(badge.name, ".htaccess");
			assertEquals(badge.description, "Earned 400 upvotes for answers in the .htaccess tag");
			assertEquals(badge.awardCount, 1);
			assertEquals(badge.tagBased, true);
			assertEquals(badge.badgesRecipientsUrl, "/badges/204");
		}
		
		[Ignore]
		[Test]
		public function testParseDateWithNaN():void
		{
			var number:Number;
			var d:Date = _parser.parseDate(number);
			assertNull(d);
		}
		
		[Test]
		public function testParseUser():void
		{
			var rawUser:String = '{   "users": [     {       "user_id": 133840,       "user_type": "registered",       "creation_date": 1246906195,       "display_name": "www.Flextras.com",       "reputation": 7618,       "email_hash": "7913289408c866314b3e4e7c478e4c78",       "age": 35,       "last_access_date": 1293940348,       "website_url": "http://www.flextras.com/blog/",       "location": "USA",       "question_count": 1,       "answer_count": 564,       "view_count": 1078,       "up_vote_count": 134,       "down_vote_count": 17,       "association_id": "945dfa0c-27a8-49cc-9de2-403b137f7c71",       "user_questions_url": "/users/133840/questions",       "user_answers_url": "/users/133840/answers",       "user_favorites_url": "/users/133840/favorites",       "user_tags_url": "/users/133840/tags",       "user_badges_url": "/users/133840/badges",       "user_timeline_url": "/users/133840/timeline",       "user_mentioned_url": "/users/133840/mentioned",       "user_comments_url": "/users/133840/comments",       "user_reputation_url": "/users/133840/reputation",       "badge_counts": {         "gold": 2,         "silver": 5,         "bronze": 12       }     }   ] }';
			var userList:UserList = _parser.parseUsers(JSON.decode(rawUser));
			var user:User = userList.users[0];
			assertEquals(user.acceptRate, 0);
			assertEquals(user.age, 35);
			assertEquals(user.answerCount, 564);
			assertEquals(user.associationId, "945dfa0c-27a8-49cc-9de2-403b137f7c71");
			assertEquals(user.badgeCounts.bronze, 12);
			assertEquals(user.badgeCounts.silver, 5);
			assertEquals(user.badgeCounts.gold, 2);
			assertEquals(user.creationDate.time, _parser.parseDate(1246906195));
			assertEquals(user.displayName, "www.Flextras.com");
			assertEquals(user.downVoteCount, 17);
			assertEquals(user.emailHash, "7913289408c866314b3e4e7c478e4c78");
			assertEquals(user.lastAccessDate.time, _parser.parseDate(1293940348));
			assertEquals(user.location, "USA");
			assertEquals(user.questionCount, 1);
			assertEquals(user.reputation, 7618);
			assertNull(user.timedPenaltyDate);
			assertEquals(user.upVoteCount, 134);
			assertEquals(user.userAnswersUrl, "/users/133840/answers");
			assertEquals(user.userBadgesUrl, "/users/133840/badges");
			assertEquals(user.userCommentsUrl, "/users/133840/comments");
			assertEquals(user.userFavoritesUrl, "/users/133840/favorites");
			assertEquals(user.userId, 133840);
			assertEquals(user.userMentionedUrl, "/users/133840/mentioned");
			assertEquals(user.userQuestionsUrl, "/users/133840/questions");
			assertEquals(user.userReputationUrl, "/users/133840/reputation");
			assertEquals(user.userTagsUrl, "/users/133840/tags");
			assertEquals(user.userTimelineUrl, "/users/133840/timeline");
			assertEquals(user.userType, UserType.REGISTERED);
			assertEquals(user.viewCount, 1078);
			assertEquals(user.websiteUrl, "http://www.flextras.com/blog/");
		}
		
		[Test]
		public function parseQuestions():void
		{
			var rawQuestions:String = '{   "total": 1273072,   "page": 1,   "pagesize": 30,   "questions": [] }';
			var questionList:QuestionList = _parser.parseQuestions(JSON.decode(rawQuestions));
			assertEquals(questionList.total, 1273072);
			assertEquals(questionList.page, 1);
			assertEquals(questionList.pageSize, 30);
		}
		
		[Test]
		public function parseQuestionWithoutArgument():void
		{
			var rawQuestion:String = '{   "total": 1,   "page": 1,   "pagesize": 30,   "questions": [     {       "tags": [         "flex",         "accessibility",         "jaws"       ],       "answer_count": 2,       "accepted_answer_id": 4344888,       "favorite_count": 0,       "bounty_amount": 50,       "question_timeline_url": "/questions/4300049/timeline",       "question_comments_url": "/questions/4300049/comments",       "question_answers_url": "/questions/4300049/answers",       "question_id": 4300049,       "owner": {         "user_id": 192705,         "user_type": "registered",         "display_name": "Ben",         "reputation": 1698,         "email_hash": "e1b7ea11d5fac04149c334a12049ab30"       },       "creation_date": 1290991339,       "last_edit_date": 1291355028,       "last_activity_date": 1291374224,       "up_vote_count": 3,       "down_vote_count": 0,       "view_count": 95,       "score": 3,       "community_owned": false,       "title": "Flex Accessiblity - Tab Focus goes out of flex app"     }   ] }';
			var questionList:QuestionList = _parser.parseQuestions(JSON.decode(rawQuestion));
			var question:Question = questionList.questions[0];
			assertEquals(question.acceptedAnswerId, 4344888);
			assertEquals(question.answerCount, 2);
			assertEquals(question.bountyAmount, 50);
			assertEquals(question.communityOwned, false);
			assertEquals(question.creationDate.time, _parser.parseDate(1290991339));
			assertEquals(question.downVoteCount, 0);
			assertEquals(question.favoriteCount, 0);
			assertEquals(question.lastActivityDate.time, _parser.parseDate(1291374224));
			assertEquals(question.owner.userId, 192705);
			assertEquals(question.owner.userType, UserType.REGISTERED);
			assertEquals(question.owner.displayName, "Ben");
			assertEquals(question.owner.reputation, 1698);
			assertEquals(question.owner.emailHash, "e1b7ea11d5fac04149c334a12049ab30");
			assertEquals(question.questionAnswersUrl, "/questions/4300049/answers");
			assertEquals(question.questionCommentsUrl, "/questions/4300049/comments");
			assertEquals(question.questionId, 4300049);
			assertEquals(question.questionTimelineUrl, "/questions/4300049/timeline");
			assertEquals(question.score, 3);
			assertEquals(question.title, "Flex Accessiblity - Tab Focus goes out of flex app");
			assertEquals(question.tags.length, 3);
			assertEquals(question.upVoteCount , 3);
			assertEquals(question.viewCount , 95);
		}
		
		[Test]
		public function parseQuestionWithAnswerArgument():void
		{
			var rawQuestion:String = '{   "total": 1,   "page": 1,   "pagesize": 30,   "questions": [     {       "tags": [         "flex",         "accessibility",         "jaws"       ],       "answer_count": 2,       "answers": [         {           "answer_id": 4344888,           "accepted": true,           "answer_comments_url": "/answers/4344888/comments",           "question_id": 4300049,           "owner": {             "user_id": 93163,             "user_type": "registered",             "display_name": "Florian F",             "reputation": 2032,             "email_hash": "a4e67dd01fd0b06d7f46d6dba99596d9"           },           "creation_date": 1291374224,           "last_activity_date": 1291374224,           "up_vote_count": 2,           "down_vote_count": 0,           "view_count": 96,           "score": 2,           "community_owned": false,           "title": "Flex Accessiblity - Tab Focus goes out of flex app"         },         {           "answer_id": 4343672,           "accepted": false,           "answer_comments_url": "/answers/4343672/comments",           "question_id": 4300049,           "owner": {             "user_id": 271725,             "user_type": "registered",             "display_name": "alxx",             "reputation": 2957,             "email_hash": "19adef5dcf23468f9643eb91c79c221d"           },           "creation_date": 1291364657,           "last_activity_date": 1291364657,           "up_vote_count": 0,           "down_vote_count": 0,           "view_count": 96,           "score": 0,           "community_owned": false,           "title": "Flex Accessiblity - Tab Focus goes out of flex app"         }       ],       "accepted_answer_id": 4344888,       "favorite_count": 0,       "bounty_amount": 50,       "question_timeline_url": "/questions/4300049/timeline",       "question_comments_url": "/questions/4300049/comments",       "question_answers_url": "/questions/4300049/answers",       "question_id": 4300049,       "owner": {         "user_id": 192705,         "user_type": "registered",         "display_name": "Ben",         "reputation": 1698,         "email_hash": "e1b7ea11d5fac04149c334a12049ab30"       },       "creation_date": 1290991339,       "last_edit_date": 1291355028,       "last_activity_date": 1291374224,       "up_vote_count": 3,       "down_vote_count": 0,       "view_count": 96,       "score": 3,       "community_owned": false,       "title": "Flex Accessiblity - Tab Focus goes out of flex app"     }   ] }';
			var questionList:QuestionList = _parser.parseQuestions(JSON.decode(rawQuestion));
			var question:Question = questionList.questions[0];
			assertEquals(question.answers.length, 2);
		}
	}
}